package org.zachary.creditbusiness.commodityandhouse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zachary.creditbusiness.commodityandhouse.entity.House;
import org.zachary.creditbusiness.commodityandhouse.service.HouseService;

import java.util.List;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@RestController
@RequestMapping("/houses")
public class HouseController {

    @Autowired
    private HouseService houseService;

    @GetMapping("/{id}")
    public House getHouse(@PathVariable Integer id) {
        return houseService.getHouseById(id);
    }

    // 保留原有的不分页接口
    @GetMapping
    public List<House> getAllHouses() {
        return houseService.getAllHouses();
    }

    // 分页获取房源列表 - 主要使用这个
    @GetMapping("/list")
    public Page<House> getHouses(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return houseService.getHouses(page, size);
    }

    @PostMapping
    public House createHouse(@RequestBody House house) {
        return houseService.createHouse(house);
    }

    @PutMapping("/{id}")
    public House updateHouse(@PathVariable Integer id, @RequestBody House house) {
        house.setId(id);
        return houseService.updateHouse(house);
    }

    @DeleteMapping("/{id}")
    public void deleteHouse(@PathVariable Integer id) {
        houseService.deleteHouse(id);
    }

    // 按标题搜索 - 支持分页
    @GetMapping("/searchByTitle")
    public Page<House> searchByTitle(
            @RequestParam String title,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return houseService.searchByTitle(title, page, size);
    }

    // 按地区筛选 - 支持分页
    @GetMapping("/filterByDistrict")
    public Page<House> filterByDistrict(
            @RequestParam String district,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return houseService.filterByDistrict(district, page, size);
    }

    // 按价格范围筛选 - 支持分页
    @GetMapping("/filterByPriceRange")
    public Page<House> filterByPriceRange(
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return houseService.filterByPriceRange(minPrice, maxPrice, page, size);
    }

    // 按租赁类型筛选 - 支持分页
    @GetMapping("/filterByRentalType")
    public Page<House> filterByRentalType(
            @RequestParam String rentalType,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return houseService.filterByRentalType(rentalType, page, size);
    }

    // 综合筛选接口 - 支持多条件同时筛选
    @GetMapping("/filter")
    public Page<House> filterHouses(
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String district,
            @RequestParam(required = false) String rentalType,
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return houseService.filterHouses(title, district, rentalType, minPrice, maxPrice, page, size);
    }
}